

#include "HeaderFiles.h"




/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void iic_gpio_init(uint32_t IIC_NUM)
{
	
	rcu_periph_clock_enable(RCU_GPIOB);
	
	if(IIC_NUM == I2C0)
	{
		rcu_periph_clock_enable(RCU_I2C0);		
		gpio_pin_remap_config(GPIO_I2C0_REMAP, ENABLE);
		gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
		gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
	}
	
	else if(IIC_NUM == I2C1)
	{
		rcu_periph_clock_enable(RCU_I2C1);
		gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
		gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_11);			
	}	
	
}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void iic_resgiter_init(uint32_t IIC_NUM)
{

    /* configure I2C0 clock */
    i2c_clock_config(IIC_NUM, 800000, I2C_DTCY_2);
    /* configure I2C0 address */
    i2c_mode_addr_config(IIC_NUM, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0xD4);
    /* enable I2C0 */
    i2c_enable(IIC_NUM);
    /* enable acknowledge */
    i2c_ack_config(IIC_NUM, I2C_ACK_ENABLE);

}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
int8_t iic_read_byte(uint32_t IIC_NUM, int8_t addr, int8_t reg)
{
	
	int8_t rece_data = 0;
	
#if 1	
    /* wait until I2C bus is idle */
    while(i2c_flag_get(IIC_NUM, I2C_FLAG_I2CBSY));

    /* send a start condition to I2C bus */
    i2c_start_on_bus(IIC_NUM);

    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_SBSEND));
	
#endif	
	
    /* send slave address to I2C bus*/
    i2c_master_addressing(IIC_NUM, addr, I2C_TRANSMITTER);
	
    /* wait until ADDSEND bit is set*/
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_ADDSEND));
	
    /* clear ADDSEND bit */
    i2c_flag_clear(IIC_NUM, I2C_FLAG_ADDSEND);
	
	/* send a data byte */
	i2c_data_transmit(IIC_NUM, reg);	
	
	
    /* wait until BTC bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_BTC));
    
    /* send a start condition to I2C bus */
    i2c_start_on_bus(IIC_NUM);
    
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_SBSEND));
	
	 /* send slave address to I2C bus */
    i2c_master_addressing(IIC_NUM, addr, I2C_RECEIVER);

	i2c_ack_config(IIC_NUM,I2C_ACK_DISABLE);
	
    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_ADDSEND));
    
    /* clear the ADDSEND bit */
    i2c_flag_clear(IIC_NUM,I2C_FLAG_ADDSEND);
	
	/* wait until the RBNE bit is set */
	while(!i2c_flag_get(IIC_NUM, I2C_FLAG_RBNE));
	/* read a data from I2C_DATA */
	rece_data = i2c_data_receive(IIC_NUM);
	
    /* send a stop condition to I2C bus*/
    i2c_stop_on_bus(IIC_NUM);
    /* wait until stop condition generate */ 
    while(I2C_CTL0(IIC_NUM)&0x0200);
	
    /* enable acknowledge */
    i2c_ack_config(IIC_NUM,I2C_ACK_ENABLE);

    i2c_ackpos_config(IIC_NUM,I2C_ACKPOS_CURRENT);

	
	return rece_data;

}

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void iic_buffer_read(uint32_t IIC_NUM, uint8_t* p_buffer, uint8_t slave_address, uint8_t read_address, uint16_t number_of_byte)
{  
    /* wait until I2C bus is idle */
    while(i2c_flag_get(IIC_NUM, I2C_FLAG_I2CBSY));

    if(2 == number_of_byte){
        i2c_ackpos_config(IIC_NUM,I2C_ACKPOS_NEXT);
    }
    
    /* send a start condition to I2C bus */
    i2c_start_on_bus(IIC_NUM);
    
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_SBSEND));
    
    /* send slave address to I2C bus */
    i2c_master_addressing(IIC_NUM, slave_address, I2C_TRANSMITTER);
    
    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_ADDSEND));
    
    /* clear the ADDSEND bit */
    i2c_flag_clear(IIC_NUM,I2C_FLAG_ADDSEND);
    
    /* wait until the transmit data buffer is empty */
    while(SET != i2c_flag_get( IIC_NUM , I2C_FLAG_TBE));

    /* enable I2C0*/
    i2c_enable(IIC_NUM);
    
    /* send the  internal address to write to */
    i2c_data_transmit(IIC_NUM, read_address);  
    
    /* wait until BTC bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_BTC));
    
    /* send a start condition to I2C bus */
    i2c_start_on_bus(IIC_NUM);
    
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_SBSEND));
    
    /* send slave address to I2C bus */
    i2c_master_addressing(IIC_NUM, slave_address, I2C_RECEIVER);

    if(number_of_byte < 3){
        /* disable acknowledge */
        i2c_ack_config(IIC_NUM,I2C_ACK_DISABLE);
    }
    
    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_ADDSEND));
    
    /* clear the ADDSEND bit */
    i2c_flag_clear(IIC_NUM,I2C_FLAG_ADDSEND);
    
    if(1 == number_of_byte){
        /* send a stop condition to I2C bus */
        i2c_stop_on_bus(IIC_NUM);
    }
    
    /* while there is data to be read */
    while(number_of_byte){
        if(3 == number_of_byte){
            /* wait until BTC bit is set */
            while(!i2c_flag_get(IIC_NUM, I2C_FLAG_BTC));

            /* disable acknowledge */
            i2c_ack_config(IIC_NUM,I2C_ACK_DISABLE);
        }
        if(2 == number_of_byte){
            /* wait until BTC bit is set */
            while(!i2c_flag_get(IIC_NUM, I2C_FLAG_BTC));
            
            /* send a stop condition to I2C bus */
            i2c_stop_on_bus(IIC_NUM);
        }
        
        /* wait until the RBNE bit is set and clear it */
        if(i2c_flag_get(IIC_NUM, I2C_FLAG_RBNE)){
            /* read a byte from the EEPROM */
            *p_buffer = i2c_data_receive(IIC_NUM);
            
            /* point to the next location where the byte read will be saved */
            p_buffer++; 
            
            /* decrement the read bytes counter */
            number_of_byte--;
        } 
    }
    
    /* wait until the stop condition is finished */
    while(I2C_CTL0(IIC_NUM)&0x0200);
    
    /* enable acknowledge */
    i2c_ack_config(IIC_NUM,I2C_ACK_ENABLE);

    i2c_ackpos_config(IIC_NUM,I2C_ACKPOS_CURRENT);
}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/


void iic_byte_write(uint32_t IIC_NUM, uint8_t slave_address, uint8_t write_address, uint8_t p_buffer)
{
    /* wait until I2C bus is idle */
    while(i2c_flag_get(IIC_NUM, I2C_FLAG_I2CBSY));

    /* send a start condition to I2C bus */
    i2c_start_on_bus(IIC_NUM);
    
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_SBSEND));
    
    /* send slave address to I2C bus */
    i2c_master_addressing(IIC_NUM, slave_address, I2C_TRANSMITTER);
    
    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_ADDSEND));
    
    /* clear the ADDSEND bit */
    i2c_flag_clear(IIC_NUM,I2C_FLAG_ADDSEND);
    
    /* wait until the transmit data buffer is empty */
    while(SET != i2c_flag_get(IIC_NUM, I2C_FLAG_TBE));
    
    /* send the EEPROM's internal address to write to : only one byte address */
    i2c_data_transmit(IIC_NUM, write_address);
    
    /* wait until BTC bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_BTC));
    
    /* send the byte to be written */
    i2c_data_transmit(IIC_NUM, p_buffer); 
    
    /* wait until BTC bit is set */
    while(!i2c_flag_get(IIC_NUM, I2C_FLAG_BTC));

    /* send a stop condition to I2C bus */
    i2c_stop_on_bus(IIC_NUM);
    
    /* wait until the stop condition is finished */
    while(I2C_CTL0(IIC_NUM)&0x0200);
}




/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void iic_init(void)
{
	iic_gpio_init(I2C0);
	iic_gpio_init(I2C1);
	
	iic_resgiter_init(I2C0);
	iic_resgiter_init(I2C1);

}



